package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.*;
import org.csu.mypetstore.api.persistence.*;
import org.csu.mypetstore.api.service.MessageService;
import org.csu.mypetstore.api.vo.MessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("messageService")
public class MessageServiceImpl implements MessageService {
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    ContactDataMapper contactDataMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    GroupMapper groupMapper;

    //根据用户名获取该用户所有的历史消息
    @Override
    public CommonResponse<List<MessageVO>> getHistoryMessage(String username) {
        //首先获取私聊信息：
        QueryWrapper<Message> messageQueryWrapper = new QueryWrapper<>();
        messageQueryWrapper.eq("fromUserId",username)
                .or().eq("toContactId",username).orderByAsc("sendTime");
        List<Message> messageList_1 = this.messageMapper.selectList(messageQueryWrapper);
        List<Message> messageList = new ArrayList<>();
        //判断该消息是不是群聊，不是群聊，则将消息加入其中
        for(Message message:messageList_1){
            if(groupMapper.selectById(message.getToContactId())==null)
                messageList.add(message);
        }
        //接下来开始获取群聊消息
        //首先获取该用户的加入的所有的群
        QueryWrapper<ContactData> contactDataQueryWrapper = new QueryWrapper<>();
        contactDataQueryWrapper.eq("userid",username);
        contactDataQueryWrapper.eq("token","群组");
        List<ContactData> contactDataList = this.contactDataMapper.selectList(contactDataQueryWrapper);
        //随后获取所有发给这些群的信息
        for(ContactData contactData:contactDataList){
            QueryWrapper<Message> messageQueryWrapper1 = new QueryWrapper<>();
            messageQueryWrapper1.eq("toContactId",contactData.getFriendId()).orderByAsc("sendTime");
            List<Message> messageList2 = this.messageMapper.selectList(messageQueryWrapper1);
            for(Message message :messageList2){
//                message.setToContactId(username);           //-------此处将
                messageList.add(message);
            }
        }
        //------------------
        List<MessageVO> messageVOList = new ArrayList<>();
        for(Message message : messageList){
            //获取消息发送者的信息：
            ChatUser user=new ChatUser();
            Account account = accountMapper.selectById(message.getFromUserId());
            user.setId(account.getUsername());
            user.setDisplayName(account.getUsername());
            user.setAvatar("https://p5.ssl.qhimgs1.com/t01ba6ea141fbcb6e8f.jpg");
//            封装MessageVO：
            messageVOList.add(message.toVO(message,user.toVO(user)));
            System.out.println("status："+message.getType());
        }
        return CommonResponse.createForSuccess(messageVOList);
    }

    //更新某个对话的历史信息
    @Override
    public CommonResponse<MessageVO> updateHistoryMessage(MessageVO messageVO) {
        //在Message表内插入新的消息
        this.messageMapper.insert(Message.toMessage(messageVO));
        //更新Contact表内的lastContent
        QueryWrapper<ContactData> contactDataQueryWrapper = new QueryWrapper<>();
        String username = messageVO.getFromUser().getId();
        Group group = groupMapper.selectById(messageVO.getToContactId());
        if(group == null){  //此时为私发消息
            System.out.println("这是一条私法消息");
//            contactDataQueryWrapper.ne("token","群组");
            contactDataQueryWrapper.eq("userid",messageVO.getFromUser().getId());
            contactDataQueryWrapper.eq("friendId",messageVO.getToContactId());
            contactDataQueryWrapper.or().eq("friendId",messageVO.getFromUser().getId());
            contactDataQueryWrapper.eq("userid",messageVO.getToContactId());
//            contactDataQueryWrapper.and(wrapper->
//                    wrapper.eq("userid",username).or().eq("friendId",username));
        }
        else{   //此时为群发消息
            System.out.println("这是一条群发消息");
//            contactDataQueryWrapper.eq("userid",username);
            contactDataQueryWrapper.eq("friendId",messageVO.getToContactId());
        }
        ContactData contactData = new ContactData();
        contactData.setLastContent((String)messageVO.getContent());
        this.contactDataMapper.update(contactData,contactDataQueryWrapper);

        return CommonResponse.createForSuccess();
    }



}
